//
//  Pupil.m
//  test
//
//  Created by Christian Speich on 11.01.08.
//  Copyright 2008 Christian Speich. All rights reserved.
//

#import "Pupil.h"
#import "Subject.h"

@implementation Pupil

- (id) init
{
	self = [super init];
	if (self != nil) {
		subjects = [[NSMutableArray alloc] initWithCapacity:1];
		currentHalfYear = 0;
	}
	return self;
}


- (NSString*) name
{
	return [name copy];
}

- (void) setName:(NSString*)name_
{
	if (name)
		[name release];
	
	name = [name_ copy];
}

- (NSString*) class
{
	return [class copy];
}

- (void) setClass:(NSString*)class_
{
	if (class)
		[class release];
	
	class = [class_ copy];
}

- (NSArray*) subjects
{
	return [subjects copy];
}

- (void) addSubject:(Subject*)subject
{
	[subjects addObject:[subject retain]];
}

- (int) currentHalfYear
{
	return currentHalfYear;
}

- (void) setCurrentHalfYear:(int)halfYear_
{
	currentHalfYear = halfYear_;
}

- (NSString*)xml
{
	return [self xmlWithOffset:0];
}

- (NSString*)xmlWithOffset:(int)i
{
	int y;
	int count;
	NSMutableString* xml = [[NSMutableString alloc] initWithCapacity:50];
	NSMutableString* offset = [[NSMutableString alloc] initWithCapacity:i];
	
	for (y = 0; y < i; y++) {
		[offset appendString:@" "];
	}
	
	[xml appendFormat:@"%@<pupil>\n", offset];
	[xml appendFormat:@"%@    <name>%@</name>\n", offset, name];
	[xml appendFormat:@"%@    <class>%@</class>\n", offset, class];
	[xml appendFormat:@"%@    <currentHalfYear>%i</currentHalfYear>\n\n", offset, currentHalfYear+1];
	
	count = [subjects count];
	for (y = 0; y < count; y++) {
		Subject* obj = [subjects objectAtIndex:y];
		[xml appendString:[obj xmlWithOffset:i+4]];
	}
	[xml appendFormat:@"%@</pupil>", offset];
	
	[offset release];
	
	return xml;
}

- (void) dealloc
{
	[subjects release];
	[name release];
	[class release];
	
	[super dealloc];
}


@end
